<template>
  <div class="xtx-checkbox" @click="toggleFn">
    <!-- 选中小图标 -->
    <i v-if="checked" class="iconfont icon-checked"></i>
    <!-- 未选中小图标 -->
    <i v-else class="iconfont icon-unchecked"></i>
    <!-- 自定义文字 -->
    <span>
      <slot />
    </span>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
export default {
  name: 'XtxCheckBox',
  props: {
    value: {
      type: Boolean,
      defalut: false
    }
  },
  setup (props, { emit }) {
    const checked = ref(false)

    function toggleFn () {
      checked.value = !checked.value
      emit('update', checked.value)
    }

    watch(() => {
      return props.value
    }, () => {
      checked.value = props.value
    }, {
      immediate: true
    })

    return {
      toggleFn,
      checked
    }
  }

}
</script>
<style scoped lang="less">
.xtx-checkbox {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: @xtxColor;
    ~ span {
      color: @xtxColor;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>
